<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formTeacherBasicRef"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormTeacherBasic()" @reset="resetFormTeacherBasic">
        <el-form-item label="隶属单位号">
          <el-input
            class="filter-item"
            v-model="formFilter.lsdwhFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
        <el-form-item label="编制类别码">
          <el-input
            class="filter-item"
            v-model="formFilter.bzlbmFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
        <el-form-item label="当前状态码">
          <el-input
            class="filter-item"
            v-model="formFilter.dqztmFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
        <el-form-item label="是否上师大委培生">
          <el-input
            class="filter-item"
            v-model="formFilter.sfssdwpsFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="formTeacherBasicTable"
      class="page-table"
      :data="formTeacherBasicTableWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((formTeacherBasicTableWidgetCurrentPage - 1) * formTeacherBasicTableWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="formTeacherBasicTableWidget.onSortChange"
      @refresh="formTeacherBasicTableWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          @click="onAddTeacherBasicClick()"
          >
          新建
        </el-button>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          @click="onExportTeacherBasicClick()"
          >
          导出
        </el-button>
        <el-upload
          class="btn-import"
          :auto-upload="false"
          action=""
          :show-file-list="false"
          accept=".xls,.xlsx"
          style="display: inline-block;"
          :on-change="onImportTeacherBasicClick"
        >
          <template #trigger>
            <el-button
              type="primary"
              :size="layoutStore.defaultFormItemSize"
            >
              导入
            </el-button>
          </template>
        </el-upload>
      </template>
      <vxe-column title="序号" type="seq" fixed="left" :index="formTeacherBasicTableWidget.getTableIndex" :width="80" />
      <vxe-column title="工号" field="gh" />
      <vxe-column title="隶属单位号" field="lsdwh" />
      <vxe-column title="进入现工作单位日期" field="jrxgzdwrq">
        <template v-slot="scope">
          <span>{{formatDateByStatsType(scope.row.jrxgzdwrq, 'day')}}</span>
        </template>
      </vxe-column>
      <vxe-column title="编制类别码" field="bzlbm" />
      <vxe-column title="当前状态码" field="dqztm" />
      <vxe-column title="是否上师大委培生" field="sfssdwps" />
      <vxe-column title="更新时间" field="updateTime">
        <template v-slot="scope">
          <span>{{formatDateByStatsType(scope.row.updateTime, 'day')}}</span>
        </template>
      </vxe-column>
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onEditTeacherBasicClick(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onDeleteTeacherBasicClick(scope.row)"
          >
            删除
          </el-button>
        </template>
      </vxe-column>
      <template slot="empty">
        <div class="table-empty unified-font">
          <img src="@/assets/img/empty.png">
          <span>暂无数据</span>
        </div>
      </template>
      <!-- 分页 -->
      <template #pagination>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            :total="formTeacherBasicTableWidgetTotalCount"
            :current-page="formTeacherBasicTableWidgetCurrentPage"
            :page-size="formTeacherBasicTableWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="formTeacherBasicTableWidget.onCurrentPageChange"
            @size-change="formTeacherBasicTableWidget.onPageSizeChange">
          </el-pagination>
        </el-row>
      </template>
    </table-box>
    <label v-if="subPage" class="page-close-box" @click="onCancel()">
      <img src="@/assets/img/back2.png" alt="">
    </label>
  </div>
</template>

<script lang="ts">
export default {
  name: 'formTeacherBasic',
};
</script>

<script setup lang="ts">
import * as validateRules from '@/common/utils/validate';
import { VxeColumn, VxeTable } from 'vxe-table';
import { ANY_OBJECT } from '@/types/generic';
import { DictData, DictionaryBase } from '@/common/staticDict/types';
import { ElMessage, ElMessageBox, UploadFile } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { useCommon } from '@/common/hooks/useCommon';
import { useLayoutStore, useStaticDictStore } from '@/store';
import { useDownload } from '@/common/hooks/useDownload';
import { useDropdown } from '@/common/hooks/useDropdown';
import { DropdownOptions, ListData } from '@/common/types/list';
import { useTable } from '@/common/hooks/useTable';
import { TableOptions } from '@/common/types/pagination';
import { useUpload } from '@/common/hooks/useUpload';
import { useUploadWidget } from '@/common/hooks/useUploadWidget';
import { DictionaryController } from '@/api/system';
import { treeDataTranslate, findItemFromList, findTreeNodePath, findTreeNode, stringCase } from '@/common/utils';
import { TeacherBasicData } from '@/api/generated/teacherBasicController';
import { TeacherBasicController } from '@/api/generated';
import FormEditTeacherBasic from '@/pages/personnelBasic/formEditTeacherBasic.vue';
import DeptSelect from '@/components/DeptSelect/index.vue';
import UserSelect from '@/components/UserSelect/index.vue';

const router = useRouter();
const route = useRoute();
const layoutStore = useLayoutStore();
const { downloadFile } = useDownload();
const { getUploadHeaders, getUploadActionUrl, fileListToJson, parseUploadData, getPictureList } = useUpload();
const { 
  Delete,
  Search,
  Edit,
  Plus,
  Refresh,
  Picture,
  Dialog,
  mainContextHeight,
  clientHeight,
  checkPermCodeExist,
  parseParams,
  parseArrayParams,
  formatDateByStatsType,
  getDateRangeFilter,
} = useCommon();
// 静态字典
const { staticDict: StaticDict } = useStaticDictStore();

const props = withDefaults(
  defineProps<{
    subPage?: number | string | boolean;
  }>(),
  {
    subPage: 0,
  },
);

const formFilter = reactive({
  // 隶属单位号
  lsdwhFilter: undefined,
  // 编制类别码
  bzlbmFilter: undefined,
  // 当前状态码
  dqztmFilter: undefined,
  // 是否上师大委培生
  sfssdwpsFilter: undefined,
});
const formFilterCopy = reactive({
  // 隶属单位号
  lsdwhFilter: undefined,
  // 编制类别码
  bzlbmFilter: undefined,
  // 当前状态码
  dqztmFilter: undefined,
  // 是否上师大委培生
  sfssdwpsFilter: undefined,
});
const instance = getCurrentInstance();

const onCancel = () => {
  router.go(-1);
  layoutStore.removeCachePage(route.fullPath as string);
  route.meta.refreshParentCachedPage = true;
};

const onResume = () => {
  refreshFormTeacherBasic();
};

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadFormTeacherBasicTableWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    teacherBasicDtoFilter: {
      lsdwh: formFilter.lsdwhFilter,
      bzlbm: formFilter.bzlbmFilter,
      dqztm: formFilter.dqztmFilter,
      sfssdwps: formFilter.sfssdwpsFilter,
    }
  };
  return new Promise((resolve, reject) => {
    TeacherBasicController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadFormTeacherBasicTableVerify = () => {
  formFilterCopy.lsdwhFilter = formFilter.lsdwhFilter;
  formFilterCopy.bzlbmFilter = formFilter.bzlbmFilter;
  formFilterCopy.dqztmFilter = formFilter.dqztmFilter;
  formFilterCopy.sfssdwpsFilter = formFilter.sfssdwpsFilter;
  return true;
};
/**
 * 新建
 */
const onAddTeacherBasicClick = (row?: TeacherBasicData) => {
  let params: ANY_OBJECT = {
  };

  Dialog
    .show('新建', FormEditTeacherBasic, { area: '900px' }, { ...params, subPage: true })
    .then(res => {
      formTeacherBasicTableWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 编辑
 */
const onEditTeacherBasicClick = (row?: TeacherBasicData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
  };

  Dialog
    .show('编辑', FormEditTeacherBasic, { area: '900px' }, { ...params, subPage: true })
    .then(res => {
      formTeacherBasicTableWidget.refreshTable();
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
};
/**
 * 导出
 */
const onExportTeacherBasicClick = (row?: TeacherBasicData) => {
  let params: ANY_OBJECT = {
  };

  TeacherBasicController.export(params, '表格组件.xlsx').then(res => {
    ElMessage.success('导出成功');
  }).catch(e => {
    ElMessage.error(e.errorMessage);
  });
};
/**
 * 导入
 */
const onImportTeacherBasicClick = (file) => {
  let params: ANY_OBJECT = {
    importFile: file.raw,
    // 是否忽略表头
    skipHeader: false
  };

  TeacherBasicController.import(params).then(res => {
    ElMessage.success('导入成功');
    formTeacherBasicTableWidget.refreshTable();
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
/**
 * 删除
 */
const onDeleteTeacherBasicClick = (row?: TeacherBasicData) => {
  let params: ANY_OBJECT = {
    id: row?.id,
  };

  ElMessageBox.confirm('是否删除此记录？').then(res => {
    TeacherBasicController.delete(params).then(res => {
      ElMessage.success('删除成功');
      formTeacherBasicTableWidget.refreshTable(false, 1);
    }).catch(e => {
      // TODO: 异常处理
      console.error(e);
    });
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const formTeacherBasicTableOptions: TableOptions<TeacherBasicData> = {
  loadTableData: loadFormTeacherBasicTableWidgetData,
  verifyTableParameter: loadFormTeacherBasicTableVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const formTeacherBasicTable = ref();
const formTeacherBasicTableWidget = useTable(formTeacherBasicTableOptions);
const {
  dataList: formTeacherBasicTableWidgetDataList,
  currentPage: formTeacherBasicTableWidgetCurrentPage,
  pageSize: formTeacherBasicTableWidgetPageSize,
  totalCount: formTeacherBasicTableWidgetTotalCount,
} = formTeacherBasicTableWidget;
const refreshFormTeacherBasic = () => {
  // 刷新段落
  formTeacherBasicTableWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormTeacherBasic = () => {
  formFilter.lsdwhFilter = undefined;
  formFilterCopy.lsdwhFilter = undefined;
  formFilter.bzlbmFilter = undefined;
  formFilterCopy.bzlbmFilter = undefined;
  formFilter.dqztmFilter = undefined;
  formFilterCopy.dqztmFilter = undefined;
  formFilter.sfssdwpsFilter = undefined;
  formFilterCopy.sfssdwpsFilter = undefined;
  refreshFormTeacherBasic();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormTeacherBasic();
};
const formInit = () => {
  refreshFormTeacherBasic();
};

onMounted(() => {
  formInit();
});

onActivated(() => {
  onResume();
});
</script>